
*for REStat paper, "Breastfeeding and Children's Early Cognitive Outcomes."

libname dd 'X:\Donnanew';

data x;
  set dd.mdid2;

proc sort;  by pcg97id;  run;

data y;
set dd.pind08;

pcg97id = er30001*1000 + er30002;

proc sort;  by pcg97id;

data z;
merge x y;  by pcg97id;  


if childid ne .;

data dd.momed08; 
set z;
keep childid mommob momyob momhgc mhgc97;

momhgc = .;

**doing for biological mother as primary caregiver;
if relpcg97 = 1 then do;

if childyob = 1983 then do;
   if er30413 gt 0 and er30413 lt 18 then momhgc = er30413;
end;
if childyob = 1984 then do;
   if (er30433 gt 0 and er30433 lt 18) then momhgc = er30433;
   else if (er30413 gt 0 and er30413 lt 18) then momhgc = er30413;
   else if (er30478 gt 0 and er30478 lt 18) then momhgc = er30478;
   else if (er30384 gt 0 and er30384 lt 18) then momhgc = er30384;
   else if (er30513 gt 0 and er30513 lt 18) then momhgc = er30513;
end;

if childyob = 1985 then do;
   if (er30478 gt 0 and er30478 lt 18) then momhgc = er30478;
   else if (er30433 gt 0 and er30433 lt 18) then momhgc = er30433;
   else if (er30513 gt 0 and er30513 lt 18) then momhgc = er30513;
   else if (er30413 gt 0 and er30413 lt 18) then momhgc = er30413;
   else if (er30549 gt 0 and er30549 lt 18) then momhgc = er30549;
end;

if childyob = 1986 then do;
   if (er30513 gt 0 and er30513 lt 18) then momhgc = er30513;
   else if (er30478 gt 0 and er30478 lt 18) then momhgc = er30478;
   else if (er30549 gt 0 and er30549 lt 18) then momhgc = er30549;
   else if (er30433 gt 0 and er30433 lt 18) then momhgc = er30433;
   else if (er30584 gt 0 and er30584 lt 18) then momhgc = er30584;
 end;

if childyob = 1987 then do;
   if (er30549 gt 0 and er30549 lt 18) then momhgc = er30549;
   else if (er30513 gt 0 and er30513 lt 18) then momhgc = er30513;
   else if (er30584 gt 0 and er30584 lt 18) then momhgc = er30584;
   else if (er30478 gt 0 and er30478 lt 18) then momhgc = er30478;
   else if (er30620 gt 0 and er30620 lt 18) then momhgc = er30620;
    end;

if childyob = 1988 then do;
   if (er30584 gt 0 and er30584 lt 18) then momhgc = er30584;
   else if (er30549 gt 0 and er30549 lt 18) then momhgc = er30549;
   else if (er30620 gt 0 and er30620 lt 18) then momhgc = er30620;
   else if (er30513 gt 0 and er30513 lt 18) then momhgc = er30513;
   else if (er30657 gt 0 and er30657 lt 18) then momhgc = er30657;
     end;

  if childyob = 1989 then do;
   if (er30620 gt 0 and er30620 lt 18) then momhgc = er30620;
   else if (er30584 gt 0 and er30584 lt 18) then momhgc = er30584;
   else if (er30657 gt 0 and er30657 lt 18) then momhgc = er30657;
   else if (er30549 gt 0 and er30549 lt 18) then momhgc = er30549;
   else if (er30703 gt 0 and er30703 lt 18) then momhgc = er30703;
       end;

  if childyob = 1990 then do;
  if (er30657 gt 0 and er30657 lt 18) then momhgc = er30657;
  else if (er30620 gt 0 and er30620 lt 18) then momhgc = er30620;
  else if (er30703 gt 0 and er30703 lt 18) then momhgc = er30703;
  else if (er30584 gt 0 and er30584 lt 18) then momhgc = er30584;
  else if (er30748 gt 0 and er30748 lt 18) then momhgc = er30748;
       end;

  if childyob = 1991 then do;
  if (er30703 gt 0 and er30703 lt 18) then momhgc = er30703;
  else if (er30657 gt 0 and er30657 lt 18) then momhgc = er30657;
  else if (er30748 gt 0 and er30748 lt 18) then momhgc = er30748;
  else if (er30620 gt 0 and er30620 lt 18) then momhgc = er30620;
  else if (er30820 gt 0 and er30820 lt 18) then momhgc = er30820;
*fill in a few more;
   if (momhgc = . and er33115 gt 0 and er33115 lt 18) then momhgc = er33115;
    end;

if childyob = 1992 then do;
  if (er30748 gt 0 and er30748 lt 18) then momhgc = er30748;
  else if (er30703 gt 0 and er30703 lt 18) then momhgc = er30703;
  else if (er30820 gt 0 and er30820 lt 18) then momhgc = er30820;
  else if (er30657 gt 0 and er30657 lt 18) then momhgc = er30657;
  else if (er33115 gt 0 and er33115 lt 18) then momhgc = er33115;
  *fill in a few more;
  if (momhgc = . and er33215 gt 0 and er33215 lt 18) then momhgc = er33215;
end;

if childyob = 1993 then do;
  if (er30820 gt 0 and er30820 lt 18) then momhgc = er30820;
  else if (er30748 gt 0 and er30748 lt 18) then momhgc = er30748;
  else if (er33115 gt 0 and er33115 lt 18) then momhgc = er33115;
  else if (er30703 gt 0 and er30703 lt 18) then momhgc = er30703;
  else if (er33215 gt 0 and er33215 lt 18) then momhgc = er33215;
  *fill in a few more;
  if (momhgc = . and er33315 gt 0 and er33315 lt 18) then momhgc = er33315;
end;

if childyob = 1994 then do;
  if (er33115 gt 0 and er33115 lt 18) then momhgc = er33115;
  else if (er30820 gt 0 and er30820 lt 18) then momhgc = er30820;
  else if (er33215 gt 0 and er33215 lt 18) then momhgc = er33215;
  else if (er30748 gt 0 and er30748 lt 18) then momhgc = er30748;
  else if (er33315 gt 0 and er33315 lt 18) then momhgc = er33315;
  *fill in a few more;
  if (momhgc = . and er33415 gt 0 and er33415 lt 18) then momhgc = er33415;
end;

if childyob = 1995 then do;
  if (er33215 gt 0 and er33215 lt 18) then momhgc = er33215;
  else if (er33115 gt 0 and er33115 lt 18) then momhgc = er33115;
  else if (er33315 gt 0 and er33315 lt 18) then momhgc = er33315;
  else if (er30820 gt 0 and er30820 lt 18) then momhgc = er30820;
  else if (er33415 gt 0 and er33415 lt 18) then momhgc = er33415;
  *fill in a few more;
  if (momhgc = . and er33516 gt 0 and er33516 lt 18) then momhgc = er33516;
end;

if childyob = 1996 then do;
  if (er33315 gt 0 and er33315 lt 18) then momhgc = er33315;
  else if (er33215 gt 0 and er33215 lt 18) then momhgc = er33215;
  else if (er33415 gt 0 and er33415 lt 18) then momhgc = er33415;
  else if (er33115 gt 0 and er33115 lt 18) then momhgc = er33115;
  else if (er33516 gt 0 and er33516 lt 18) then momhgc = er33516;
    end;

if childyob = 1997 then do;
  if (er33415 gt 0 and er33415 lt 18) then momhgc = er33415;
  else if (er33315 gt 0 and er33315 lt 18) then momhgc = er33315;
  else if (er33516 gt 0 and er33516 lt 18) then momhgc = er33516;
  else if (er33215 gt 0 and er33215 lt 18) then momhgc = er33215;
  end;

**for 1997 interview, may use to fill in so many missings;
mhgc97 = .;
if er33415 gt 0 and er33415 lt 18 then mhgc97 = er33415;


**here, will try to do month/year of birth, but can also get it from pcg file, recall;

mommob = .;
momyob = .;
if er33405 gt 0 and er33406 gt 0 then do;
   mommob = er33405;
   momyob = er33406;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er33305 gt 0 and er33306 gt 0 then do;
   mommob = er33305;
   momyob = er33306;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er33505 gt 0 and er33506 gt 0 then do;
   mommob = er33505;
   momyob = er33506;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er33605 gt 0 and er33606 gt 0 then do;
   mommob = er33605;
   momyob = er33606;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er33705 gt 0 and er33706 gt 0 then do;
   mommob = er33705;
   momyob = er33706;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er33805 gt 0 and er33806 gt 0 then do;
   mommob = er33805;
   momyob = er33806;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er33905 gt 0 and er33906 gt 0 then do;
   mommob = er33905;
   momyob = er33906;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er33205 gt 0 and er33206 gt 0 then do;
   mommob = er33205;
   momyob = er33206;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er33105 gt 0 and er33106 gt 0 then do;
   mommob = er33105;
   momyob = er33106;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30810 gt 0 and er30811 gt 0 then do;
   mommob = er30810;
   momyob = er30811;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30737 gt 0 and er30738 gt 0 then do;
   mommob = er30737;
   momyob = er30738;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30693 gt 0 and er30694 gt 0 then do;
   mommob = er30693;
   momyob = er30694;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30646 gt 0 and er30647 gt 0 then do;
   mommob = er30646;
   momyob = er30647;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30610 gt 0 and er30611 gt 0 then do;
   mommob = er30610;
   momyob = er30611;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30574 gt 0 and er30575 gt 0 then do;
   mommob = er30574;
   momyob = er30575;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30539 gt 0 and er30540 gt 0 then do;
   mommob = er30539;
   momyob = er30540;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30502 gt 0 and er30503 gt 0 then do;
   mommob = er30502;
   momyob = er30503;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30467 gt 0 and er30468 gt 0 then do;
   mommob = er30467;
   momyob = er30468;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
if mommob = . and momyob = . and er30433 gt 0 and er30434 gt 0 then do;
   mommob = er30433;
   momyob = er30434;
end;
if momyob = 9999 then do;
   mommob = .;
   momyob = .;
end;
*found the 9999 at end, so just put throughout, so can fill in;
end;
**next, do same thing for fathers ed., and go from there;

proc means;
run;
